const crypto = require('crypto');
const key = Buffer.from('9vApxLk5G3PAsJrM', 'utf8');
const iv = Buffer.from('FnJL7EDzjqWjcaY9', 'utf8');
// 加密
function enCrypt(src) {
    let sign = '';
    // 注这里的密钥必须是8/16/32位，如果加密算法是128，则对应的密钥是16位，如果加密算法是256，则对应的密钥是32位
    const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);//参数 1.加密算法，加密秘钥，加密时所用的向量 第三个参数可选
    sign += cipher.update(src, 'utf8', 'hex');
    sign += cipher.final('hex');
    return sign;
}

// 解密
function deCode(sign) {
    let src = '';
    const cipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    src += cipher.update(sign, 'hex', 'utf8');
    src += cipher.final('utf8');
    return src;
}
module.exports = { enCrypt, deCode };
